package com.lehecai.ucenter.export;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

import com.lehecai.core.util.CoreDateUtils;
import com.lehecai.ucenter.entity.permission.User;

/**
 * 导出
 * @author likunpeng
 *
 */
public class UsersExport {

	public static Workbook export(List<User> userList) throws Exception {
		if (userList == null || userList.size() == 0) {
			return null;
		}
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet worksheet = workbook.createSheet();
		workbook.setSheetName(0, "sheet1");
		
		//单元格样式
		worksheet.setDefaultColumnWidth(20);
		
		Row row1 = worksheet.createRow(0);
		Cell row1cell0 = row1.createCell((short) 0,Cell.CELL_TYPE_STRING);
		Cell row1cell1 = row1.createCell((short) 1,Cell.CELL_TYPE_STRING);
		Cell row1cell2 = row1.createCell((short) 2,Cell.CELL_TYPE_STRING);
		Cell row1cell3 = row1.createCell((short) 3,Cell.CELL_TYPE_STRING);
		Cell row1cell4 = row1.createCell((short) 4,Cell.CELL_TYPE_STRING);
		Cell row1cell5 = row1.createCell((short) 5,Cell.CELL_TYPE_STRING);
		Cell row1cell6 = row1.createCell((short) 6,Cell.CELL_TYPE_STRING);
		Cell row1cell7 = row1.createCell((short) 7,Cell.CELL_TYPE_STRING);

		row1cell0.setCellValue("编号");
		row1cell1.setCellValue("用户名");
		row1cell2.setCellValue("姓名");
		row1cell3.setCellValue("手机号码");
		row1cell4.setCellValue("电话");
		row1cell5.setCellValue("邮箱");
		row1cell6.setCellValue("创建时间");
		row1cell7.setCellValue("修改时间");
		
		for (int i = 0; i < userList .size(); i++) {
			User user = userList.get(i);
        	Row row = worksheet.createRow(i + 1);
        	Cell rowcell0 = row.createCell((short) 0,Cell.CELL_TYPE_STRING);
    		Cell rowcell1 = row.createCell((short) 1,Cell.CELL_TYPE_STRING);
    		Cell rowcell2 = row.createCell((short) 2,Cell.CELL_TYPE_STRING);
    		Cell rowcell3 = row.createCell((short) 3,Cell.CELL_TYPE_STRING);
    		Cell rowcell4 = row.createCell((short) 4,Cell.CELL_TYPE_STRING);
    		Cell rowcell5 = row.createCell((short) 5,Cell.CELL_TYPE_STRING);
    		Cell rowcell6 = row.createCell((short) 6,Cell.CELL_TYPE_STRING);
    		Cell rowcell7 = row.createCell((short) 7,Cell.CELL_TYPE_STRING);

    		rowcell0.setCellValue(user.getId() + "");
    		rowcell1.setCellValue(user.getUsername());
    		rowcell2.setCellValue(user.getName());
    		rowcell3.setCellValue(user.getMobile());
    		rowcell4.setCellValue(user.getTel());
    		rowcell5.setCellValue(user.getEmail());
    		rowcell6.setCellValue(CoreDateUtils.formatDate(user.getCreatedTime(), CoreDateUtils.DATETIME));
    		rowcell7.setCellValue(CoreDateUtils.formatDate(user.getUpdatedTime(), CoreDateUtils.DATETIME));
        }
		return workbook;
	}
}
